﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTOService;
using BUSService;

namespace QuanLyBangDia
{
    public partial class frmTimKiemBangDia : Form
    {
        //Thien code
        MyService myservice = new MyService();
        private int mintTotalRecords = 0;
        private int mintPageSize = 0;
        private int mintPageCount = 0;
        private int mintCurrentPage = 1;
        BangDia bangdia = new BangDia();

        public frmTimKiemBangDia()
        {
            InitializeComponent();
            this.Icon = QuanLyBangDia.Properties.Resources.HDD;
        }

        private void frmTimKiemBangDia_Load(object sender, EventArgs e)
        {
            try
            {
                txtTenBangDia.Focus();
                //Load TenBangDia
                BangDia[] arrBD = myservice.SelectBangDia();                
                AutoCompleteStringCollection arrCollection = new AutoCompleteStringCollection();
                foreach (BangDia bd in arrBD)
                {
                    arrCollection.Add(bd.TenBD);
                }
                txtTenBangDia.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtTenBangDia.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtTenBangDia.AutoCompleteCustomSource = arrCollection;

                //Load LoaiSanPham                
                LoaiSanPham[] arrLSP = myservice.SelectLoaiSanPham();
                cbLoaiSanPham.DataSource = arrLSP;              
                cbLoaiSanPham.DisplayMember = "TenLoaiSanPham";
                cbLoaiSanPham.ValueMember = "MaLoaiSanPham";
                cbLoaiSanPham.SelectedIndex = -1;

                //Load LoaiBangDia
                LoaiBangDia[] arrLBD = myservice.SelectLoaiBangDia();
                cbLoaiBangDia.DataSource = arrLBD;
                cbLoaiBangDia.DisplayMember = "TenLoaiBangDia";
                cbLoaiBangDia.ValueMember = "MaLoaiBangDia";
                cbLoaiBangDia.SelectedIndex = -1;

                //Load TinhTrang
                TinhTrang[] arrTT = myservice.SelectTinhTrang();
                cbTinhTrang.DataSource = arrTT;
                cbTinhTrang.DisplayMember = "TenTinhTrang";
                cbTinhTrang.ValueMember = "MaTinhTrang";
                cbTinhTrang.SelectedIndex = -1;
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            try
            {
                lbAlert.Text = "";
                bangdia = new BangDia();
                bangdia.TenBD = txtTenBangDia.Text;
                if(cbLoaiBangDia.SelectedIndex!=-1)
                    bangdia.LoaiBD = int.Parse(cbLoaiBangDia.SelectedValue.ToString());
                if(cbLoaiSanPham.SelectedIndex!=-1)
                    bangdia.LoaiSanPham = int.Parse(cbLoaiSanPham.SelectedValue.ToString());
                if (cbTinhTrang.SelectedIndex != -1)
                    bangdia.TinhTrang = int.Parse(cbTinhTrang.SelectedValue.ToString());

                this.mintPageSize = 2; //page size
                this.mintTotalRecords = myservice.NumberBangDia(bangdia);
                this.mintPageCount = this.mintTotalRecords / this.mintPageSize;
                if (this.mintTotalRecords % this.mintPageSize > 0)
                    this.mintPageCount++;
                this.mintCurrentPage = 0;

                LoadPage();                                
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.mintCurrentPage == this.mintPageCount)
                    this.mintCurrentPage = this.mintPageCount - 1;
                this.mintCurrentPage--;
                if (this.mintCurrentPage < 1)
                    this.mintCurrentPage = 0;
                LoadPage();
            }
            catch
            {
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                this.mintCurrentPage++;
                if (this.mintCurrentPage > (this.mintPageCount - 1))
                    this.mintCurrentPage = this.mintPageCount - 1;
                LoadPage();
            }
            catch
            {
            }
        }
        private void LoadPage()
        {
            try
            {
                int intSkip = 0;
                intSkip = (this.mintCurrentPage * this.mintPageSize);
                BangDia[] arr = myservice.TimKiemBangDia(bangdia, mintPageSize, intSkip);                                               
                dgvDanhSach.Rows.Clear();
                foreach (BangDia item in arr)
                {
                    LoaiBangDia lbd = myservice.GetLoaiBangDia(item.LoaiBD);
                    LoaiSanPham lsp = myservice.GetLoaiSanPham(item.LoaiSanPham);
                    TinhTrang tt = myservice.GetTinhTrang(item.TinhTrang);
                    Object[] o = { item.MaBD.ToString(), item.TenBD, lbd.TenLoaiBangDia, lsp.TenLoaiSanPham, item.GiaBD, item.SoLuong.ToString(), tt.TenTinhTrang, item.MoTa };
                    dgvDanhSach.Rows.Add(o);
                }
                dgvDanhSach.Show();
                lbPage.Text = (this.mintCurrentPage + 1).ToString() + " / " + this.mintPageCount.ToString();
                if (arr.Length == 0)
                    lbAlert.Text = "Kết quả không được tìm thấy!!";
            }
            catch (Exception ex)
            {

            }
        }
    }
}